package cn.stylefeng.guns.modular.info.service;

import cn.hutool.core.util.StrUtil;
import cn.stylefeng.guns.modular.common.service.CommonService;
import cn.stylefeng.guns.modular.info.entity.CpSG;
import cn.stylefeng.guns.modular.info.mapper.CpSGMapper;
import cn.stylefeng.guns.wechat.WeChatMsgSend;
import cn.stylefeng.guns.wechat.WeChatUrlData;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @program: football_manage
 * @description:
 * @author: xiesm
 * @create: 2021-10-27 13:36
 **/
@Service
public class CpSGService extends ServiceImpl<CpSGMapper, CpSG> {

    @Autowired(required = false)
    private CpSGMapper cpSGMapper;
    @Autowired
    private CommonService commonService;

    private List<CpSG> getSGs() {
        LambdaQueryWrapper<CpSG> query = new LambdaQueryWrapper();
        List<CpSG> cpSGs = cpSGMapper.selectList(query.apply("1=1 order by id desc LIMIT 10"));
        return cpSGs;
    }

    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");


    public void warn() throws IOException {
        numToNumWarn();
        repeatWarn();
        repeatWarn2();
        repeatWarn3();
    }

    private Integer[] check1 = {5, 7};
    private Integer[] check2 = {2, 9};

    /**
     * 号码到号码
     *
     * @throws IOException
     */
    public void numToNumWarn() throws IOException {
        List<CpSG> cpSGs = getSGs();
        for (int j = 0; j < check1.length; j++) {
            Integer num1 = check1[j];
            Integer num2 = check2[j];
            CpSG cpSG = cpSGs.get(2);
            List<Integer> nums = cpSG.getNums();
            int index = 0;
            for (int i = 0; i < nums.size(); i++) {
                if (nums.get(i) == num1) {
                    index = i;
                    break;
                }
            }
            CpSG sg = cpSGs.get(0);
            sg.getNums().set(index, 2);
            Integer num = sg.getNums().get(index);
            if (num == num2) {
                String msg = StrUtil.format("{},{},触发{}->{}", sg.getId(), sdf.format(sg.getDate()), num1, num2);
                send(sg, msg);
            }
        }
    }

    public void repeatWarn() throws IOException {
        List<CpSG> cpSGs = getSGs();
        CpSG cpSG0 = cpSGs.get(0);
        CpSG cpSG1 = cpSGs.get(1);
        CpSG cpSG2 = cpSGs.get(2);
        CpSG cpSG3 = cpSGs.get(3);
        List<Integer> nums1 = cpSG1.getNums();
        List<Integer> nums2 = cpSG2.getNums();
        for (int i = 0; i < nums1.size(); i++) {
            Integer compareVal = null; //重复值
            Integer nowVal = null; //现在值
            //获取重复值和当前的现在值，用于比较
            if (Objects.equals(nums1.get(i), nums2.get(i))) {
                compareVal = nums1.get(i);
                nowVal = cpSG0.getNums().get(i);
            }
            if (compareVal == null && nowVal == null) {
                continue;
            }
            for (int j = 0; j < cpSG3.getNums().size(); j++) {
                if (Objects.equals(cpSG3.getNums().get(j), compareVal)) {
                    if (Objects.equals(cpSG2.getNums().get(j), nowVal)) {
                        String msg = StrUtil.format("{},{},触发重复值:{}", cpSG0.getId(), sdf.format(cpSG0.getDate()), compareVal);
                        send(cpSG0, msg);
                    }
                }
            }
        }
    }

    public void repeatWarn2() throws IOException {
        List<CpSG> cpSGs = getSGs();
        CpSG cpSG0 = cpSGs.get(0);
        CpSG cpSG1 = cpSGs.get(1);
        CpSG cpSG2 = cpSGs.get(2);
        List<Integer> nums0 = cpSG0.getNums();
        List<Integer> nums1 = cpSG1.getNums();
        List<Integer> nums2 = cpSG2.getNums();
        for (int i = 0; i < nums1.size(); i++) {
            if (nums0.get(i) == nums1.get(i) && nums1.get(i) == nums2.get(i)) {
                String msg = StrUtil.format("{},{},连续3次重复值:{}", cpSG0.getId(), sdf.format(cpSG0.getDate()), nums0.get(i));
                send(cpSG0, msg);
            }
        }
    }

    public void repeatWarn3() throws IOException {
        List<CpSG> cpSGList = cpSGMapper.selectList(null);
        List<String> msgs = new ArrayList<>();
        int i = 2;
        CpSG cpSG0 = cpSGList.get(i);
        CpSG cpSG1 = cpSGList.get(i + 1);
        CpSG cpSG2 = cpSGList.get(i + 2);
        List<Integer> nums0 = cpSG0.getNums();
        List<Integer> nums1 = cpSG1.getNums();
        List<Integer> nums2 = cpSG2.getNums();
        for (int j = 0; j < nums1.size(); j++) {
            Integer num = nums0.get(j);
            if (nums0.get(j) == nums1.get(j) && nums1.get(j) == nums2.get(j)) {
                Integer numIndex = cpSGList.get(i - 1).getNumIndex(num);
                if (cpSGList.get(i - 2).getNumFromIndex(numIndex) == num) {
                    String msg = StrUtil.format("{},{},触发连续3次重复值:{}", cpSG0.getId(), sdf.format(cpSG0.getDate()), nums0.get(j));
                    msgs.add(msg);
                }
            }
        }
    }

//    public void repeatWarn3() throws IOException {
//        List<CpSG> cpSGList = cpSGMapper.selectList(null);
//        List<String> msgs = new ArrayList<>();
//        for (int i = 2; i < cpSGList.size() - 3; i++) {
//            CpSG cpSG0 = cpSGList.get(i);
//            CpSG cpSG1 = cpSGList.get(i + 1);
//            CpSG cpSG2 = cpSGList.get(i + 2);
//            List<Integer> nums0 = cpSG0.getNums();
//            List<Integer> nums1 = cpSG1.getNums();
//            List<Integer> nums2 = cpSG2.getNums();
//            for (int j = 0; j < nums1.size(); j++) {
//                Integer num = nums0.get(j);
//                if (nums0.get(j) == nums1.get(j) && nums1.get(j) == nums2.get(j)) {
//                    Integer numIndex = cpSGList.get(i - 1).getNumIndex(num);
//                    if (cpSGList.get(i - 2).getNumFromIndex(numIndex) == num) {
//                        String msg = StrUtil.format("{},{},触发连续3次重复值:{}", cpSG0.getId(), sdf.format(cpSG0.getDate()), nums0.get(j));
//                        msgs.add(msg);
//                    } else {
//                        String msg = StrUtil.format("{},{},没触发连续3次重复值:{}", cpSG0.getId(), sdf.format(cpSG0.getDate()), nums0.get(j));
//                        msgs.add(msg);
//                    }
//                }
//            }
//        }
//        System.out.println(JSONObject.toJSONString(msgs));
//    }


    public void send(CpSG sg, String warnMsg) throws IOException {
        Integer appid = 1000014;
        WeChatMsgSend swx = new WeChatMsgSend();
        String postdata = swx.createpostdata("13459446228", "text", appid, "content", warnMsg);
        swx.post("utf-8", WeChatMsgSend.CONTENT_TYPE, (new WeChatUrlData()).getSendMessage_Url(), postdata, commonService.getWechatToken(appid));
    }


    public void check() throws Exception {
        int ckeck1 = 2;
        int ckeck2 = 5;

        List<CpSG> sgs = cpSGMapper.selectList(new LambdaQueryWrapper<CpSG>().orderByDesc(CpSG::getId));
        for (int i = 0; i < sgs.size() - 2; i++) {
            CpSG sg = sgs.get(i);
            List<Integer> nums = sg.getNums();
            Integer index = 0;
            for (int j = 0; j < nums.size(); j++) {
                if (nums.get(j) == ckeck1) {
                    index = j;
                }
            }
            CpSG sg2 = sgs.get(i + 2);
            if (sg2.getNums().get(index) == ckeck2) {
                System.out.println(sg2.getId());
            }
        }
    }

    public void check2() throws Exception {
        List<CpSG> sgs = cpSGMapper.selectList(new LambdaQueryWrapper<CpSG>().orderByDesc(CpSG::getId));
        for (int i = 1; i < sgs.size() - 2; i++) {
            CpSG sg = sgs.get(i);
            List<Integer> nums = sg.getNums();
            for (int j = 0; j < nums.size(); j++) {
                getCF(sgs, nums.get(j), i, j);
            }
        }
    }

    public void getCF(List<CpSG> sgs, int num, int i, int index) {
        Integer num2 = sgs.get(i + 1).getNums().get(index);
        if (num == num2 && index != 9) {
            Integer num3 = sgs.get(i + 2).getNums().get(index + 1);
            Integer num4 = sgs.get(i - 1).getNums().get(index);
            if (num3 == num4) {
                System.out.println(sgs.get(i - 1).getId());
            }
        }
    }


}
